

*	This code generates results for Figure 1;

options threads cpucount=8 sortsize=32G bufno=8;
dm log 'clear';

%let mainfolder=D:\Dropbox\Characteristics Factors\RFS_Replication\;

libname mylib "&mainfolder.\Data_Cleaned";

proc datasets library=work nolist kill;
run;
quit;

data crspd/view=crspd;
set mylib.crsp_d;
where year(date)=2007 and 7<=month(date)<=8;
if (dlstcd = 500 | dlstcd <= 584 and dlstcd >= 520) & hexcd = 3 & (dlret = .S | dlret = .T | dlret = .P) then Ddlret = -.55;
else if (dlstcd = 500 | dlstcd <= 584 and dlstcd >= 520) & (hexcd = 1 | hexcd =2 ) & (dlret = .S | dlret = .T | dlret = .P) then Ddlret = -.3;
else if dlret = .S | dlret = .T | dlret = .A | dlret = .P then Ddlret = .;
else DDLRET=DLRET;
if Dlstcd=. or dlstcd=100 THEN ret=rET; 
ELSE ret=ddlret;
day=date;
date=intnx('month',date,0,'e');
format date yymmddn8.;
me=abs(prc)*shrout/1000;*ME in millions;
keep permno date day me ret;
Run;

proc sort data=crspd out=crspd1 nodupkey;
by _all_;
where not missing(me);
run;

proc sort data=crspd1 nodupkey;
by permno day descending ret;
run;

proc sort data=crspd1 nodupkey;
by permno day;
run;

data crspd1(keep=permno date day lagme ret);
set crspd1;
by permno;
lagme=lag(me);
if first.permno then lagme=.;
run;

proc sort data=crspd1 nodupkey;
by permno date day;
where year(date)=2007 and month(date)=8;
run;

data ret;
set mylib.crsp;
where year(date)>=1950;
if (dlstcd = 500 | dlstcd <= 584 and dlstcd >= 520) & hexcd = 3 & (dlret = .S | dlret = .T | dlret = .P) then Ddlret = -.55;
else if (dlstcd = 500 | dlstcd <= 584 and dlstcd >= 520) & (hexcd = 1 | hexcd =2 ) & (dlret = .S | dlret = .T | dlret = .P) then Ddlret = -.3;
else if dlret = .S | dlret = .T | dlret = .A | dlret = .P then Ddlret = .;
else DDLRET=DLRET;
if Dlstcd=. or dlstcd=100 THEN ret=rET; 
ELSE ret=ddlret;
date=intnx('month',date,0,'e');
format date yymmddn8.;
me=abs(prc)*shrout/1000;*ME in millions;
prc=abs(prc);
keep permno date me ret prc exchcd shrcd siccd;
Run;

proc sort data=ret nodupkey;
by permno date descending ret;
run;

proc sort data=ret nodupkey;
by permno date;
run;

data lagme(keep=permno date lagme lagprc lagexchcd lagshrcd lagsiccd);
set ret;
date=intnx('month',date,1,'e');
format date yymmddn8.;
rename me=lagme prc=lagprc exchcd=lagexchcd shrcd=lagshrcd siccd=lagsiccd;
run;

proc sort data=lagme nodupkey;
by permno date;
where not missing(permno) and not missing(date) and not missing(lagme);
run;

data compu_crsp(drop=date rename=(date1=date));
set mylib.char(keep=permno date mom);
date1=intnx('month',date,1,'e');*move to crsp timing;
format date1 yymmddn8.;
run;

%let minday=45;
data beta(keep=permno date beta1_umd);
set mylib.beta2_3;
date=intnx('month',date,1,'e');
format date yymmddn8.;
if freq_umd<&minday then beta1_umd=.;
run;

proc sort data=beta nodupkey;
by permno date;
run;

data all;
merge lagme(in=in2) compu_crsp(in=in3) beta(in=in4);
by permno date;
if in2 and in3;
run;

data all1;
set all;
where 1<=lagexchcd<=3 and 10<=lagshrcd<=11 and year(date)=2007 and month(date)=8;
run;
*	momentum port;
data mom;
set all1;
where not missing(mom);
keep permno date mom;
run;

proc rank data=mom out=mom group=10 ties=dense;
by date;
var mom;
ranks momgroup;
run;

proc sort data=mom out=mom1(rename=(momgroup=port1)) nodupkey;
by permno date;
where momgroup in (0,9);
run;

data mom1(keep=permno day port1 lagme ret rename=(day=date));
merge mom1(keep=permno date port1 in=in1) crspd1(in=in2);
by permno date;
if in1;
run;

proc sort data=mom1;
by date port1;
where not missing(date) and not missing(port1);
run;

proc means data=mom1 noprint;
where not missing(lagme);
var ret;
by date port1;
weight lagme;
output out=mom2(drop=_freq_ _type_) mean=ret;
run;

proc sql;
create table mom3
as select distinct a.date,(a.ret-b.ret) as mom
from mom2 as a,mom2 as b 
where a.date=b.date and a.port1=9 and b.port1=0
order by a.date;
quit;
*	momentum beta port;
data mombeta;
set all1;
where not missing(beta1_umd);
keep permno date beta1_umd;
run;

proc rank data=mombeta out=mombeta group=10 ties=dense;
by date;
var beta1_umd;
ranks mombetagroup;
run;

proc sort data=mombeta out=mombeta1(rename=(mombetagroup=port1)) nodupkey;
by permno date;
where mombetagroup in (0,9);
run;

data mombeta1(keep=permno day port1 lagme ret rename=(day=date));
merge mombeta1(keep=permno date port1 in=in1) crspd1(in=in2);
by permno date;
if in1;
run;

proc sort data=mombeta1;
by date port1;
where not missing(date) and not missing(port1);
run;

proc means data=mombeta1 noprint;
where not missing(lagme);
var ret;
by date port1;
weight lagme;
output out=mombeta2(drop=_freq_ _type_) mean=ret;
run;

proc sql;
create table mombeta3
as select distinct a.date,(a.ret-b.ret) as mombeta
from mombeta2 as a,mombeta2 as b 
where a.date=b.date and a.port1=9 and b.port1=0
order by a.date;
quit;
*	merge mom and mombeta spread;
data both;
merge mom3 mombeta3;
by date;
format date yymmddn8.;
if date<='14AUG2007'd;
if date='02AUG2007'd then mom=0;
if date='02AUG2007'd then mombeta=0;
if date>='02AUG2007'd;
run;

proc sql;
create table both1
as select distinct a.date,exp(sum(log(1+b.mom)))-1 as mom,exp(sum(log(1+b.mombeta)))-1 as mombeta
from both as a left join both as b 
on b.date<=a.date
group by a.date;
quit;

proc sort data=both1;
by date;
run;

data both2;
set both1;
format mom mombeta percentn8.2;
format date MMDDYY10.; 
run;

proc export data=both1 outfile="&mainfolder.\Result\Figure1.csv" replace;run; 
